// src/api/upgrade.api.ts
import request from "@/utils/request";

const COMMON_BASE_URL = "/api";

/**
 * 升级 API 模块
 */
export const UPGRADE_API = {
  /**
   * 获取升级进度
   * @returns Promise<UpgradeProgressResponse>
   */
  getUpgradeProgress: (): Promise<UpgradeProgressResponse> => {
    return request<void, UpgradeProgressResponse>({
      url: `${COMMON_BASE_URL}/progress`,
      method: "get",
    });
  },

  /**
   * 开始升级操作
   * @param data 升级参数
   * @returns Promise<StartUpgradeResponse>
   */
  startUpgrade: (data: StartUpgradePayload): Promise<StartUpgradeResponse> => {
    return request<StartUpgradePayload, StartUpgradeResponse>({
      url: `${COMMON_BASE_URL}/upgrade`,
      method: "post",
      params: data,
    });
  },

  /**
   * 开始回滚操作
   * @param data 回滚参数
   * @returns Promise<StartRollbackResponse>
   */
  startRollback: (data: StartRollbackPayload): Promise<StartRollbackResponse> => {
    return request<StartRollbackPayload, StartRollbackResponse>({
      url: `${COMMON_BASE_URL}/rollback`,
      method: "post",
      data,
    });
  },

  /**
   * 升级文件
   * @param data 升级文件参数
   * @returns Promise<UpgradeFileResponse>
   */
  upgradeFile: (data: UpgradeFilePayload): Promise<UpgradeFileResponse> => {
    return request<UpgradeFilePayload, UpgradeFileResponse>({
      url: `${COMMON_BASE_URL}/upgrade`,
      method: "post",
      data,
    });
  },

  /**
   * 回滚文件
   * @param data 回滚文件参数
   * @returns Promise<RollbackFileResponse>
   */
  rollbackFile: (data: RollbackFilePayload): Promise<RollbackFileResponse> => {
    return request<RollbackFilePayload, RollbackFileResponse>({
      url: `${COMMON_BASE_URL}/rollback`,
      method: "post",
      data,
    });
  },

  /**
   * 创建备份
   * @param data 备份参数
   * @returns Promise<BackupResponse>
   */
  createBackup: (data: CreateBackupPayload): Promise<BackupResponse> => {
    return request<CreateBackupPayload, BackupResponse>({
      url: `${COMMON_BASE_URL}/backup`,
      method: "post",
      data,
    });
  },

  /**
   * 查询备份点列表
   * @returns Promise<string[]>
   */
  getBackupList: (): Promise<string[]> => {
    return request<void, string[]>({
      url: `${COMMON_BASE_URL}/backup_list`,
      method: "get",
    });
  },

};

/**
 * 基础API响应接口
 */
export interface BaseApiResponse {
  success: boolean;
  message: string;
}

/**
 * 升级进度响应接口
 */
export interface UpgradeProgressResponse {
  state: "ok" | "error" | "processing";
  progress: string;
  state_name: string;
}

/**
 * 开始升级请求参数
 */
export interface StartUpgradePayload {
  filePath: string;
}

/**
 * 开始升级响应接口
 */
export interface StartUpgradeResponse extends BaseApiResponse {
  taskId?: string;
}

/**
 * 开始回滚请求参数
 */
export interface StartRollbackPayload {
  backup_name: string;
}

/**
 * 开始回滚响应接口
 */
export interface StartRollbackResponse extends BaseApiResponse {
  taskId?: string;
}

/**
 * 升级文件请求参数
 */
export interface UpgradeFilePayload {
  filePath: string;
}

/**
 * 升级文件响应接口
 */
export interface UpgradeFileResponse extends BaseApiResponse {
  taskId?: string;
}

/**
 * 回滚文件请求参数
 */
export interface RollbackFilePayload {
  filePath: string;
}

/**
 * 回滚文件响应接口
 */
export interface RollbackFileResponse extends BaseApiResponse {
  taskId?: string;
}

/**
 * 创建备份请求参数
 */
export interface CreateBackupPayload {
  filePath: string;
  backupName?: string;
}

/**
 * 创建备份响应接口
 */
export interface BackupResponse extends BaseApiResponse {
  backupName?: string;
}

/**
 * 程序信息接口
 */
export interface ProgramInfo {
  name: string;
  version: string;
  uploadTime: string;
  crc: string;
  path: string;
}

/**
 * 备份点信息接口
 */
export interface BackupPointInfo {
  name: string;
  createTime: string;
  size?: number;
  description?: string;
}

/**
 * API错误类型
 */
export enum ApiErrorType {
  NETWORK_ERROR = "NETWORK_ERROR",
  SERVER_ERROR = "SERVER_ERROR",
  VALIDATION_ERROR = "VALIDATION_ERROR",
  UNAUTHORIZED = "UNAUTHORIZED",
  FORBIDDEN = "FORBIDDEN",
  NOT_FOUND = "NOT_FOUND",
}

/**
 * API错误接口
 */
export interface ApiError {
  type: ApiErrorType;
  message: string;
  code?: string | number;
  details?: any;
}
